﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Event.Default;
using NHibernate.Event;
using SaladBar.Domain;
using NHibernate;
namespace SaladBar.Service.Repositories.NHibernateRepository
{
    public class PersistentObjectEventListener : IPreUpdateEventListener, IPreInsertEventListener
    {
        public bool OnPreUpdate(PreUpdateEvent @event)
        {
            if (@event.Entity is PersistentObject)
            {
                @event.State[@event.Persister.PropertyNames.ToList().IndexOf("UpdateDate")] = DateTime.Now;
            }
            return false;
        }

        public bool OnPreInsert(PreInsertEvent @event)
        {
            if (@event.Entity is PersistentObject)
            {
                @event.State[@event.Persister.PropertyNames.ToList().IndexOf("CreateDate")] = DateTime.Now;
                @event.State[@event.Persister.PropertyNames.ToList().IndexOf("UpdateDate")] = DateTime.Now;
            }
            return false;
        }

    }
}
